package com.epam.qanda.db.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.epam.qanda.db.ConnectionManager;
import com.epam.qanda.db.dao.BaseDao;
import com.epam.qanda.db.mapping.Converter;
import com.epam.qanda.db.model.User;
import com.epam.qanda.db.model.UserCategory;

public class UserCategoryDao implements BaseDao {
	private final String ADD_USER_CAREGORY = "INSERT INTO user_category (user_id,category_id) VALUES (?,?)";
	private final String GET_BY_ID = "SELECT * FROM user_category WHERE id=?";
	private final String GET_BY_USER_ID = "SELECT * FROM user_category WHERE user_id=?";
	private final String REMOVE_USER_CAREGORY = "DELETE FROM user_category WHERE id = ?";
	private final String UPDATE_USER_CAREGORY = "UPDATE user_category SET user_id=?, category_id=?  WHERE id = ?";
	private final String GET_ALL_USRES_BY_CATEGORY_ID = "SELECT * FROM user INNER JOIN user_category ON user_category.user_id = user.id WHERE user_category.category_id = ?";
	private final String GET_ALL_SUBSCRIBED_USRES = "SELECT DISTINCT user.id, user.role_id, user.fname, user.lname, user.login, user.`password`, user.email, user.photo, user.confirmation_status,user.rating,user.status,user.date_ban_off FROM user INNER JOIN user_category ON user_category.user_id = user.id";
	private static final Logger LOG = Logger.getLogger("stdout");
	
	@Override
	public <T> void create(T element) {
		UserCategory temp = (UserCategory) element;
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection.prepareStatement(ADD_USER_CAREGORY)) {
			preparedStatement.setInt(1, temp.getUserId());
			preparedStatement.setInt(2, temp.getCategoryId());
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T read(Integer id) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		UserCategory userCategory = null;
		try (PreparedStatement statement = connection.prepareStatement(GET_BY_ID)) {
			statement.setInt(1, id);
			ResultSet rs = statement.executeQuery();
			if (rs.next()) {
				userCategory = Converter.<UserCategory> fromResaultSetToObject(rs,
						UserCategory.class);
			}
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) userCategory;
	}

	@Override
	public void delete(Integer id) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(REMOVE_USER_CAREGORY)) {
			preparedStatement.setInt(1, id);
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}

	@Override
	public <T> void update(T element) {
		UserCategory temp = (UserCategory) element;
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(UPDATE_USER_CAREGORY)) {
			preparedStatement.setInt(5, temp.getId());
			preparedStatement.setInt(1, temp.getUserId());
			preparedStatement.setInt(2, temp.getCategoryId());
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}

	public List<UserCategory> getUserCategories(Integer userId) {
		List<UserCategory> list = new ArrayList<>();
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement statement = connection.prepareStatement(GET_BY_USER_ID)) {
			statement.setInt(1, userId);
			ResultSet rs = statement.executeQuery();
			while (rs.next()) {
				list.add(Converter.<UserCategory> fromResaultSetToObject(rs, UserCategory.class));
			}
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return list;
	}

	public List<User> getCategorieUsers(Integer categoryId) {
		List<User> users = new ArrayList<User>();
		
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement statement = connection.prepareStatement(GET_ALL_USRES_BY_CATEGORY_ID)) {
			statement.setInt(1, categoryId);
			ResultSet rs = statement.executeQuery();
			while (rs.next()) {
				users.add(Converter.<User> fromResaultSetToObject(rs, User.class));
			}
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return users;
	}
	
	public List<User> getAllSubscribedUser() {
		List<User> users = new ArrayList<User>();
		
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement statement = connection.prepareStatement(GET_ALL_SUBSCRIBED_USRES)) {
			ResultSet rs = statement.executeQuery();
			while (rs.next()) {
				users.add(Converter.<User> fromResaultSetToObject(rs, User.class));
			}
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return users;
	}
}
